﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class CompanyProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static List<Model.ReferenceTypeValue> GetAllReference(string referenceCode)
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), referenceCode);
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ReferenceTypeValue referenceValue = new Model.ReferenceTypeValue();
                    referenceValue.ID = r["ID"].ToString();
                    referenceValue.ReferenceCode = r["ReferenceCode"].ToString();
                    referenceValue.ReferenceName = r["ReferenceName"].ToString();
                    referenceValue.ReferenceValue = r["ReferenceValue"].ToString();
                    lstReferenceValue.Add(referenceValue);
                }

            }
            return lstReferenceValue;
        }
        [WebMethod]
        public static string UpdateCompanyProfile(string oCompany)
        {
            Model.Company company = JsonConvert.DeserializeObject<Model.Company>(oCompany);
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonCompanyProfile(HttpContext.Current.Session["CompanyCode"].ToString(), company.Address, company.ContactPerson, company.ContactPhone,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString());
            return errMsg;
        }
        [WebMethod]
        public static string UpdateCompanySetting(string oCompany)
        {
            Model.Company company = JsonConvert.DeserializeObject<Model.Company>(oCompany);
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonCompanySetting(HttpContext.Current.Session["CompanyCode"].ToString(), company.PointExpireType, company.PointExpireFixedDate,
                company.PointExpireDays, company.PointConflictMethod, company.PointChangeAction, company.GradeMethod,company.PromotionConflictMethod,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString());
            return errMsg;
        }
    }
}